package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.user.center.MessageData;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;

/**
 * Created by Administrator on 2016/12/16.
 */

public class MessageAdapter extends BaseAdapter {

    private Context context;
    private List<MessageData> list;
    private Handler handler;

    public MessageAdapter(Context context, List<MessageData> data, Handler handler) {
        this.context = context;
        this.list = data;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_message, parent, false);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
            holder.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
            holder.webView = (WebView) convertView.findViewById(R.id.webView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        MessageData data = list.get(position);
        if (null != data.getTitle() && !"".equals(data.getTitle())) {
            holder.tvTitle.setText(data.getTitle());
        } else {
            holder.tvTitle.setText("");
        }
        if (null != data.getCtime() && !"".equals(data.getCtime())) {
            holder.tvTime.setText(data.getCtime());
        } else {
            holder.tvTime.setText("");
        }
        if (null != data.getDescription() && !"".equals(data.getDescription())) {
            String mDescription = data.getDescription();
            if (mDescription.startsWith("<p") && mDescription.endsWith("</p>")) {
                holder.webView.setVisibility(View.VISIBLE);
                holder.tvContent.setVisibility(View.GONE);
                initWebView(holder.webView,mDescription);
            } else {
                holder.webView.setVisibility(View.GONE);
                holder.tvContent.setVisibility(View.VISIBLE);
                holder.tvContent.setText("       " + data.getDescription());
            }
        } else {
            holder.webView.setVisibility(View.GONE);
            holder.tvContent.setVisibility(View.VISIBLE);
            holder.tvContent.setText("");
        }
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                new AlertDialog.Builder(context).setTitle("您确定要删除该消息吗？")
                        .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Message msg = Message.obtain();
                                msg.arg1 = position;
                                handler.sendMessage(msg);
                                dialog.dismiss();
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();

                return false;
            }
        });
        return convertView;
    }

    private void initWebView(WebView webView, String description) {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setDisplayZoomControls(false);
        webView.getSettings().setDefaultTextEncodingName("UTF-8");
        webView.getSettings().setBlockNetworkImage(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webView.getSettings().setMixedContentMode(webView.getSettings().MIXED_CONTENT_ALWAYS_ALLOW);  //注意安卓5.0以上的权限
        }
        webView.loadDataWithBaseURL(null, getNewContent(description), "text/html", "utf-8", null);
    }

    private String getNewContent(String htmltext) {
        Document doc = Jsoup.parse(htmltext);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width", "100%").attr("height", "auto");
        }
        return doc.toString();
    }

    class ViewHolder {
        private TextView tvTitle;
        private TextView tvTime;
        private TextView tvContent;
        private WebView webView;
    }
}
